package main

import (
	"fmt"
	"github.com/buaazp/fasthttprouter"
	"github.com/valyala/fasthttp"
	"gitlab.vrviu.com/inviu/backend-go-public/gopublic/config"
	"gitlab.vrviu.com/inviu/backend-go-public/gopublic/httpwrap"
	"gitlab.vrviu.com/inviu/backend-go-public/gopublic/vlog"
	"net/http"
	_ "net/http/pprof"
	"stress_tester/cg_area_client/service"
)

func initLogger() {
	filename := "./conf/logger.json"

	c, err := vlog.NewConfig(filename)
	if err != nil {
		panic("init logger config file failed, file:" + filename + "err:" + err.Error())
	}

	vlog.SetConfig(c)
	vlog.ReplaceStdLog()
}

//func initConfig() {
//	filename := "./conf/app.conf"
//
//	if acm, err := gopublic.CreateAreaConfigManager(filename); err != nil {
//		panic(err)
//	} else {
//		gopublic.GACM = acm
//		gopublic.GCM = acm.GCM
//	}
//}

// func initServer() {
// 	// 创建kafka produer
// 	if err := kafkawrap.CreateSingletonKafkaProducer(kafkawrap.DefaultProducerConfig); err != nil {
// 		panic("create kafka producer failed. " + err.Error())
// 	}
// }

func initRouter() *fasthttprouter.Router {
	// 新建router
	router := fasthttprouter.New()

	// 添加路由: 请求更新配置信息
	router.GET("/v1/config/reload", httpwrap.ResponseWrap(service.ReloadConfig))
	//添加路由: 开始游戏
	router.POST("/v1/aclient/start", httpwrap.ResponseWrap(service.Start))
	//添加路由: 停止游戏
	//router.GET("/v1/aclient/stop", httpwrap.ResponseWrap(service.Stop))
	router.GET("/v1/server/stop", httpwrap.ResponseWrap(service.Stop))

	return router
}

func main() {
	// 初始化logger
	go func() {
		if err := http.ListenAndServe(":6061", nil); err != nil {
			fmt.Printf("Error starting pprof server: %v\n", err)
		}
	}()
	initLogger()

	// 初始化配置
	//initConfig()

	// 初始化服务
	// initServer()

	vlog.Infof("startup server on %s", config.GetConfigManager().Section("service").Key("http_host").String())

	// 启动服务
	if err := fasthttp.ListenAndServe(config.GetConfigManager().Section("service").Key("http_host").String(), initRouter().Handler); err != nil {
		vlog.Errorf("listen and serve failed: %s", err.Error())

	}
}
